# -*- coding:utf-8 -*-
"""
三洋财经
http://www.sanyangcaijing.com/
"""
import requests
import json
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def post_html_text(url, data):
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': '26',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'PHPSESSID=5q869g404qv0jvvq97t3u77it5',
        'Host': 'www.sanyangcaijing.com',
        'Origin': 'http://www.sanyangcaijing.com',
        'Referer': 'http://www.sanyangcaijing.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


data = {
    'page': '3',
    'limit': '10'
}
url = 'http://www.sanyangcaijing.com/index.php/Index/Quick/ajaxQuick'
text = post_html_text(url, data)  # 得到页面text，可进一步处理


def get_dates_zx(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    data = json.loads(text)
    dates = [date['time'][:10] for date in data]
    return dates


def get_dates_kx(url, data):
    a = 1
    dates = []
    while a < 50:
        data = {
            'page': a,
            'limit': '10'
        }
        url = 'http://www.sanyangcaijing.com/index.php/Index/Quick/ajaxQuick'
        print('----三洋财经快讯获取第{}页----'.format(a))
        a += 1
        text = json.loads(post_html_text(url, data))
        if not text:
            continue
        data = text
        dates += [date['date'][-5:] for date in data]

    return dates


def count_process_zx():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while a < 200:
        data = {
            'parentId': '1',
            'page': a,
            'limit': '15'
        }
        url = 'http://www.sanyangcaijing.com/index.php/Index/Message/messageList'
        a += 1
        print('----三洋财经资讯获取第{}页----'.format(a))
        dates = get_dates_zx(url, data)
        if not dates:
            continue
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_kx():  # 快讯
    try:
        a = 0
        l = []
        dates = get_dates_kx(url, data)
        while 1:
            if dates[a] < dates[a + 1]:
                l.append(a + 1)
            if len(l) == 3:
                break
            a += 1
        d1 = len(dates[:l[0]])
        d2 = len(dates[l[0]:l[1]])
        d3 = len(dates[l[1]:l[2]])
        return (d1, d2, d3)
    except:
        return (0, 0, 0)


def main():
    u = '三洋财经'
    t1 = count_process_kx()
    t2 = count_process_zx()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
